<script setup lang="ts">
import 'vue3-tree-org/lib/vue3-tree-org.css'
import { Vue3TreeOrg } from 'vue3-tree-org'
import { treeOrgConfig } from '../index'
import type {
  CalculationStatusMonitoringSelectCalculationStatusPageResult1 as A,
  CalculationStatusMonitoringPublicParams,
  CalculationStatusMonitoringPublicResult,
  CustomDialogInstance,
} from '@/types'
import { calculationStatusMonitoringApi } from '@/api'
// ------------------use------------------
// #region
const props = defineProps<{
  info: A['baseDataValInternalList'][number] | null
  dateType: string
}>()
const [isFullScreen, setIsFullScreen] = useState(false)
const [treeData, setTreeData]
  = useState<CalculationStatusMonitoringPublicResult | null>(
    null,
  )
const { withLoading, vLoading } = useDefineLoading({
  destroyNode: true,
})
// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
// #endregion
// ------------------computed------
// #region

// #endregion
// ------------------生命周期------------------
// #region

// #endregion
// ------------------内置方法---------------------
// #region
// #endregion
// ------------------方法---------------------
// #region
async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  getTreeData()
}
function close() {
  dialogRef.value?.close?.()
}
async function getTreeData() {
  const { dataTime, indicatorNumber } = unref(props.info)!
  const params: Omit<
    CalculationStatusMonitoringPublicParams,
    'indicatorName'
  > = {
    dataTime,
    indicatorNumber: indicatorNumber as string,
    key: props.dateType,
  }
  const { data: res } = await withLoading(
    calculationStatusMonitoringApi.selectCalculateRelationships,
  )(params)
  setTreeData(res)
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    title="指标计算关系图"
    width="800px"
    fullable
    :show-confirm-button="false"
    cancel-button-text="关闭"
    @fullscreen="setIsFullScreen"
  >
    <div class="height-fill width-fill RelationshipModal">
      <el-empty
        v-show="!treeData"
        description="请切换其他时间段查看"
      />
      <Vue3TreeOrg
        v-show="treeData"
        ref="treeOrgRef"
        v-bind="(treeOrgConfig as any)"
        :data="treeData ?? {}"
        class="!height-vh-1/2 box-border"
        :class="[
          {
            '!height-fill': isFullScreen,
          },
        ]"
      >
        <!-- 自定义节点内容 -->
        <template #default="{ node }">
          <el-popover
            placement="left"
            :width="400"
            :show-after="300"
            append-to=".RelationshipModal"
          >
            <template #reference>
              <div
                class="tree-org-node__text node-label c-pointer"
              >
                <div>{{ node.label }}</div>
                <div>
                  时间类型：{{ node.$$data.timeType }}
                </div>
              </div>
            </template>
            <el-descriptions
              title="详情"
              :column="1"
              size="small"
              border
              :min-width="50"
              class="width-fill"
            >
              <el-descriptions-item label="计算公式">
                {{ node.$$data.formulaContent }}
              </el-descriptions-item>
              <el-descriptions-item
                v-if="node.$$data.dataTime"
                label="数据时间"
              >
                {{ node.$$data.dataTime }}
              </el-descriptions-item>
              <el-descriptions-item
                v-if="node.$$data.updateTime"
                label="计算时间"
              >
                {{ node.$$data.updateTime }}
              </el-descriptions-item>
              <el-descriptions-item label="质量">
                <el-tag
                  v-if="node.$$data.dataQuality === 1"
                  size="small"
                  type="success"
                >
                  good
                </el-tag>
                <el-tag v-else size="small" type="danger">
                  good
                </el-tag>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="node.$$data.dataValue"
                label="值"
              >
                {{ node.$$data.dataValue }}
              </el-descriptions-item>
            </el-descriptions>
          </el-popover>
        </template>
      </Vue3TreeOrg>
    </div>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
